package com.example.orchid.coordinatorlayoutdemo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.orchid.coordinatorlayoutdemo.R;
import com.example.orchid.coordinatorlayoutdemo.activity.TwoActivity;
import com.example.orchid.coordinatorlayoutdemo.adapter.BookRecyclerAdapter;
import com.example.orchid.coordinatorlayoutdemo.Model.BookBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Orchid on 2015-10-23.
 */
public class BookFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    private String content;
    private View view;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout mRefreshLayout;

    private List<BookBean> beanList;
    private BookRecyclerAdapter adapter;
    //上次listView滚动到最下方时，itemId
    private int mListViewPreLast = 0;

    private LinearLayoutManager mLayoutManager;

    private String des[] = {"云层里的阳光", "好美的海滩", "好美的海滩", "夕阳西下的美景", "夕阳西下的美景"
            , "夕阳西下的美景", "夕阳西下的美景", "夕阳西下的美景", "好美的海滩"};

    private int resId[] = {R.drawable.img1, R.drawable.img2, R.drawable.img2, R.drawable.img3,
            R.drawable.img4, R.drawable.img5, R.drawable.img3, R.drawable.img1};


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.refresh_item, container, false);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        content = getArguments().getString("content");
        mRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_widget);
        mRefreshLayout.setColorSchemeResources(android.R.color.holo_green_dark, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        mRefreshLayout.setOnRefreshListener(this);
        // 这句话是为了，第一次进入页面的时候显示加载进度条
        mRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources()
                        .getDisplayMetrics()));

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE &&
                        mListViewPreLast + 1 == adapter.getItemCount()) {
                    mRefreshLayout.setRefreshing(true);
                    Toast.makeText(getActivity(), "上拉刷新", Toast.LENGTH_LONG).show();
                    BookBean bean = new BookBean();
                    bean.setResId(resId[5]);
                    bean.setTitle(des[3]);
                    bean.setSubTitle(des[3]);

                    adapter.getList().add(bean);
                    adapter.notifyDataSetChanged();
                    mRefreshLayout.setRefreshing(false);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                mListViewPreLast = mLayoutManager.findLastCompletelyVisibleItemPosition();
            }
        });

        mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        initData();
    }


    private void initData() {
        beanList = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            BookBean bean = new BookBean();
            bean.setResId(resId[i]);
            bean.setTitle(des[i]);
            bean.setSubTitle(des[i]);
            beanList.add(bean);
        }
        adapter = new BookRecyclerAdapter(getActivity(), beanList);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BookRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object object) {
                startActivity(new Intent(getActivity(), TwoActivity.class));
            }
        });
    }

    @Override
    public void onRefresh() {
        Toast.makeText(getActivity(), "下拉刷新", Toast.LENGTH_LONG).show();
        BookBean bean = new BookBean();
        bean.setResId(resId[3]);
        bean.setTitle(des[4]);
        bean.setSubTitle(des[4]);

        adapter.getList().add(bean);
        adapter.notifyDataSetChanged();
        mRefreshLayout.setRefreshing(false);
    }
}
